package com.ruoyi.project.business.monthsalary.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.monthsalary.entity.MonthSalary;
import com.ruoyi.project.business.monthsalary.request.CulMonthSalaryRequest;
import com.ruoyi.project.business.monthsalary.request.ListMonthSalaryRequest;
import com.ruoyi.project.business.monthsalary.response.MonthSalaryResponse;
import com.ruoyi.project.business.monthsalary.service.IMonthSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 月薪Controller
 * 
 * @author ${author}
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/business/monthsalary")
public class MonthSalaryController extends BaseController {
    @Autowired
    private IMonthSalaryService monthSalaryService;

    /**
     * 查询月薪列表
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:list')")
    @PostMapping("/list")
    public TableDataInfo list(ListMonthSalaryRequest request) {
        startPage();
        Long tenantId = getTenantId();
        if(request.getUserId() == null && request.getDeptId() == null) {
            request.setDeptId(SecurityUtils.getSysUser().getDeptId());
        }
        List<MonthSalaryResponse> list = monthSalaryService.selectMonthSalaryList(request, tenantId);
        return getDataTable(list);
    }


    /**
     * 导出月薪列表
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:export')")
    @Log(title = "月薪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(ListMonthSalaryRequest request) {
        Long tenantId = getTenantId();
        List<MonthSalaryResponse> list = monthSalaryService.selectMonthSalaryList(request, tenantId);
        ExcelUtil<MonthSalaryResponse> util = new ExcelUtil<>(MonthSalaryResponse.class);
        return util.exportExcel(list, "monthsalary");
    }

    /**
     * 获取月薪详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(monthSalaryService.selectMonthSalaryById(id));
    }

    /**
     * 新增月薪
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:add')")
    @Log(title = "月薪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MonthSalary monthSalary) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthSalaryService.insertMonthSalary(monthSalary, operatorId, tenantId));
    }

    /**
     * 修改月薪
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:edit')")
    @Log(title = "月薪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MonthSalary monthSalary) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthSalaryService.updateMonthSalary(monthSalary, operatorId, tenantId));
    }

    /**
     * 删除月薪
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:remove')")
    @Log(title = "月薪", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthSalaryService.deleteMonthSalaryByIds(ids, operatorId, tenantId));
    }

    /**
     * 计算工资
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:edit')")
    @PostMapping("/culUserSalary")
    public AjaxResult culUserSalary(@RequestBody CulMonthSalaryRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        BigDecimal salary = monthSalaryService.culUserSalary(request, tenantId, operatorId);
        return AjaxResult.success(salary);
    }

    /**
     * 计算部门的工资
     */
    @PreAuthorize("@ss.hasPermi('business:monthsalary:edit')")
    @PostMapping("/culDeptSalary")
    public AjaxResult culDeptSalary(@RequestBody CulMonthSalaryRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        monthSalaryService.culDeptSalary(request, tenantId, operatorId);
        return AjaxResult.success();
    }
}
